package com.IOT_LSL;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Fragment_graph extends Fragment {
    private LineChart line_chart_temp;
    private LineChart_DetailsMakerView detailsMakerView;
    private View view;

    public LineDataSet set1;
    public LineDataSet set2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_graph,container,false);
        line_chart_temp = (LineChart) view.findViewById(R.id.line_chart_view);
        set1 = ((MainActivity)getActivity()).getSet1();
        set2 = ((MainActivity)getActivity()).getSet2();
        Init_LineChart();
        ((MainActivity)getActivity()).set_set1(set1);
        ((MainActivity)getActivity()).set_set2(set2);

        CheckBox setData_temp = (CheckBox) view.findViewById(R.id.setdata_temp);
        CheckBox setData_humi = (CheckBox) view.findViewById(R.id.setdata_humi);
        setData_temp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    line_chart_temp.getLineData().getDataSets().get(0).setVisible(true);
                }else{
                    line_chart_temp.getLineData().getDataSets().get(0).setVisible(false);
                }
                line_chart_temp.invalidate();
            }
        });
        setData_humi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    line_chart_temp.getLineData().getDataSets().get(1).setVisible(true);
                }else{
                    line_chart_temp.getLineData().getDataSets().get(1).setVisible(false);
                }
                line_chart_temp.invalidate();
            }
        });

        return view;
    }

    private void Init_LineChart() {
        //获取此图表的x轴
        XAxis xAxis = line_chart_temp.getXAxis();
        //获取右边的轴线
        YAxis rightAxis=line_chart_temp.getAxisRight();
        //获取左边的轴线
        YAxis leftAxis = line_chart_temp.getAxisLeft();
        xAxis.setEnabled(true);//设置轴启用或禁用 如果禁用以下的设置全部不生效
        xAxis.setDrawAxisLine(true);//是否绘制轴线
        xAxis.setDrawGridLines(true);//设置x轴上每个点对应的线
        xAxis.setDrawLabels(true);//绘制标签  指x轴上的对应数值
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴的显示位置
        xAxis.enableGridDashedLine(1f, 1f, 0f);
        xAxis.setGranularity(1f);//x轴坐标占的宽度
        //xAxis.setLabelCount(5,false);//显示的坐标数量
        //leftAxis.setLabelCount(5,false);
        xAxis.setAxisMaximum(100f);
        xAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(100f);
        leftAxis.setAxisMinimum(0f);
        xAxis.setAvoidFirstLastClipping(false);//图表将避免第一个和最后一个标签条目被减掉在图表或屏幕的边缘
        List<String> list = new ArrayList<>();
        for(int a=0;a<=100;a++){
            list.add(String.valueOf(a).concat(":00"));
        }
        xAxis.setValueFormatter(new IndexAxisValueFormatter(list));
        //设置图表右边的y轴禁用
        rightAxis.setEnabled(false);
        //设置网格线为虚线效果
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        xAxis.enableGridDashedLine(10f, 10f, 0f);
        //是否绘制0所在的网格线
        leftAxis.setDrawZeroLine(false);
        leftAxis.setAxisMinimum(0f);//设置x轴的最小值

        line_chart_temp.setTouchEnabled(true); // 设置是否可以触摸
        line_chart_temp.setDragEnabled(true);// 是否可以拖拽
        line_chart_temp.setScaleEnabled(true);// 是否可以缩放 x和y轴, 默认是true
        line_chart_temp.setScaleXEnabled(true); //是否可以缩放 仅x轴
        line_chart_temp.setScaleYEnabled(true); //是否可以缩放 仅y轴
        line_chart_temp.setPinchZoom(true);  //设置x轴和y轴能否同时缩放。默认是否
        line_chart_temp.setDoubleTapToZoomEnabled(true);//设置是否可以通过双击屏幕放大图表。默认是true
        line_chart_temp.setHighlightPerDragEnabled(true);//能否拖拽高亮线(数据点与坐标的提示线)，默认是true
        line_chart_temp.setDragDecelerationEnabled(false);//拖拽滚动时，手放开是否会持续滚动，默认是true（false是拖到哪是哪，true拖拽之后还会有缓冲）
        line_chart_temp.setDragDecelerationFrictionCoef(0.99f);//与上面那个属性配合，持续滚动时的速度快慢，[0,1) 0代表立即停止。.setTouchEnabled(true); // 设置是否可以触摸

        Legend legend = line_chart_temp.getLegend();//图例
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setTextSize(10f);//设置文字大小
        legend.setForm(Legend.LegendForm.LINE);//正方形，圆形或线
        legend.setFormSize(10f); // 设置Form的大小
        legend.setDrawInside(false);//是否绘制在图表里面
        line_chart_temp.animateXY(1500,2500);

/////////////////////////////////////////////////        /////////////////////////////////////////////////
        Description description = new Description();
        description.setText(" ");
        description.setTextColor(Color.RED);
        description.setTextSize(10);
        line_chart_temp.setDescription(description);
        line_chart_temp.setNoDataText("正在采集数据!");
        line_chart_temp.setNoDataTextColor(Color.RED);//没有数据时显示文字的颜色
        line_chart_temp.setDrawGridBackground(false);//chart 绘图区后面的背景矩形将绘制
        line_chart_temp.setGridBackgroundColor(Color.RED);
        line_chart_temp.setDrawBorders(true);//显示边界

        ArrayList<Entry> list_set1 = new ArrayList<>();
        ArrayList<Entry> list_set2 = new ArrayList<>();
        list_set1.add(new Entry(-1,-1));
        list_set2.add(new Entry(-1,-1));

        //设置数据1  参数1：数据源 参数2：图例名称
        set1 = new LineDataSet(list_set1, "温度");
        set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set1.setColor(Color.RED);
        set1.setCircleColor(Color.RED);
        set1.setLineWidth(2f);//设置线宽
        set1.setCircleRadius(3f);//设置焦点圆心的大小
        set1.setValueTextSize(10f);//设置显示值的文字大小
        set1.setDrawFilled(true);
        //设置数据2
        set2 = new LineDataSet(list_set2, "湿度");
        set2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set2.setColor(Color.BLUE);
        set2.setCircleColor(Color.BLUE);
        set2.setLineWidth(2f);
        set2.setCircleRadius(3f);
        set2.setValueTextSize(10f);
        set2.setDrawFilled(true);

        //保存LineDataSet集合
        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        dataSets.add(set2);
        //创建LineData对象 属于LineChart折线图的数据集合
        LineData data = new LineData(dataSets);
        // 添加到图表中
        line_chart_temp.setData(data);
        line_chart_temp.setVisibleXRange(0,5);
        line_chart_temp.setVisibleYRange(0,100, YAxis.AxisDependency.LEFT);


        detailsMakerView = new LineChart_DetailsMakerView(getContext(),R.layout.linechart_marker);
        detailsMakerView.setChartView(line_chart_temp);
        line_chart_temp.setMarker(detailsMakerView);

        //绘制图表
        line_chart_temp.invalidate();
    }
}
